package com.sokal.easycare.modules.product.web.product;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.mapper.JsonMapper;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.utils.FastDFS.FastDFSClient;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.product.entity.brand.ProBrand;
import com.sokal.easycare.modules.product.entity.product.ProProduct;
import com.sokal.easycare.modules.product.entity.product.ProProductFile;
import com.sokal.easycare.modules.product.service.product.ProProductFileService;
import com.sokal.easycare.modules.product.service.product.ProProductService;
import com.sokal.easycare.modules.sys.entity.Menu;

/**
 * 产品Controller
 * 
 * @author liliangdong
 * @version 2017-03-12
 */
@Controller
@RequestMapping(value = "${adminPath}/product/product/proProduct")
public class ProProductController extends BaseController {

	@Autowired
	private ProProductService proProductService;

	@Autowired
	private ProProductFileService proProductFileService;

	@ModelAttribute
	public ProProduct get(@RequestParam(required = false) String id) {
		ProProduct entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = proProductService.get(id);
		}
		if (entity == null) {
			entity = new ProProduct();
		}
		return entity;
	}

	@RequiresPermissions("product:product:proProduct:view")
	@RequestMapping(value = { "list", "" })
	public String list(ProProduct proProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProProduct> page = proProductService.findPage(new Page<ProProduct>(request, response), proProduct);
		model.addAttribute("page", page);
		return "modules/product/product/proProductList";
	}

	@RequiresPermissions("product:product:proProduct:view")
	@RequestMapping(value = "form")
	public String form(ProProduct proProduct, Model model) {
		model.addAttribute("proProduct", proProduct);
		return "modules/product/product/proProductForm";
	}

	@RequiresPermissions("product:product:proProduct:edit")
	@RequestMapping(value = "save")
	public String save(ProProduct proProduct, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, proProduct)) {
			return form(proProduct, model);
		}
		proProductService.save(proProduct);
		addMessage(redirectAttributes, "保存产品成功");
		return "redirect:" + Global.getAdminPath() + "/product/product/proProduct/?repage";
	}

	@RequiresPermissions("product:product:proProduct:edit")
	@RequestMapping(value = "delete")
	public String delete(ProProduct proProduct, RedirectAttributes redirectAttributes) {
		proProductService.delete(proProduct);
		addMessage(redirectAttributes, "删除产品成功");
		return "redirect:" + Global.getAdminPath() + "/product/product/proProduct/?repage";
	}

	/**
	 *  跳转到产品图片页
	 * 
	 * @param proProductID
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addProductFile")
	public String addProductFile(String productId, Model model) {
		ProProduct proProduct = new ProProduct(productId);
		ProProductFile proProductFile = new ProProductFile();
		proProductFile.setProProduct(proProduct);
		List<ProProductFile> proProductFileList = proProductFileService.findList(proProductFile);
		model.addAttribute("proProductFileList", proProductFileList);
		model.addAttribute("productId", productId);
		model.addAttribute("fastdfs_file_url", Global.getConfig("fastdfs_file_url"));
		return "modules/product/product/uploadFile";
	}

	/**
	 * 保存产品图片
	 * 
	 * @param productFile
	 * @param productId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "saveProductFile")
	public String saveProductFile(@RequestParam(value = "productFile") MultipartFile productFile, String productId) {
		Map<String, Object> map = Maps.newHashMap();
		try {
			proProductFileService.save(productFile, productId);
			map.put("success", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("success", false);
		}
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
	/**
	 * 删除产品图片
	 * @param productFileId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "deleteProductFile")
	public String deleteProductFile(String productFileId) {
		Map<String, Object> map = Maps.newHashMap();
		ProProductFile proProductFile=new ProProductFile(productFileId);
		proProductFileService.delete(proProductFile);
		map.put("success", true);
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
	/**
	 * 批量修改文件排序
	 */
	@ResponseBody
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts) {
		Map<String, Object> map = Maps.newHashMap();
    	for (int i = 0; i < ids.length; i++) {
    		ProProductFile proProductFile=new ProProductFile(ids[i]);
    		proProductFile.setSort(sorts[i]);
    		proProductFileService.updateProductFileSort(proProductFile);
    	}
		map.put("success", true);
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
	/**
	 *  跳转到二维码管理页
	 * 
	 * @param proProductID
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "twoDimlCode")
	public String twoDimlCode(String productId, Model model) {
		ProProduct proProduct = proProductService.get(productId);
		model.addAttribute("proProduct", proProduct);
		model.addAttribute("fastdfs_file_url", Global.getConfig("fastdfs_file_url"));
		return "modules/product/product/twoDimlCode";
	}
	
	/**
	 *  生成二维码
	 * 
	 * @param proProductID
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "createTwoDimlCode")
	public String createTwoDimlCode(String productId, Model model,HttpServletRequest request) {
		Map<String, Object> map = Maps.newHashMap();
		try {
			proProductService.createTwoDimlCode(productId,Global.getUserfilesBaseDir(request)+Global.getConfig("twoDimlCodePath"));
			ProProduct proProduct = proProductService.get(productId);
			map.put("success", true);
			map.put("proProduct", proProduct);
			map.put("fastdfs_file_url", Global.getConfig("fastdfs_file_url"));
		} catch (Exception e) {
			map.put("success", false);
			e.printStackTrace();
		}
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<ProProduct> list = proProductService.findList(new ProProduct());
		for (int i=0; i<list.size(); i++){
			ProProduct e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()))){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}

}